﻿using System;
using System.Collections.Generic;
using System.Text;
using AppToolKit.Core.Context.Spring;
using DALFactory.DataBase;
using System.Data;
using Common.Logging;
using Utility;


namespace BLL.Business
{
    [AutoWiring]
    public class TaskFacedDao
    {
        [AutoWiring]
        public AccessHelper AccessHelperHolder { get; set; }

        ILog log = LogManager.GetLogger(LogDefault.AppInfo);

        public void SaveAlloment(string hotelid, List<string> tasklist_code)
        {
            //DeleteAlloment(hotelid, tasklist_code);
            // string strSql=string.Empty;
            // foreach (string key in tasklist_code)
            //{
            //   string id= Exist(hotelid, key, tasklist_code);
            //   if (string.IsNullOrEmpty(id))
            //   {
            //       strSql = string.Format("insert into allotment(id,tasklist_code,task_code,hotelid,[choose]) "
            //                                          + "values('{0}','{1}','{2}','{3}','{4}')", CreateNo(),
            //                                          tasklist_code, key, hotelid, true);
            //       log.Debug(string.Format("strSql:{0}", strSql));
            //       AccessHelperHolder.ExecuteNonQuery(CommandType.Text, strSql);
            //   }
            //   else
            //   {
            //       UpdateAlloment(hotelid, key, tasklist_code, "true", id);
            //   }
               
            //}
           
        }
        private string Exist(string hotelid, string task_code, string tasklist_code)
        {
            string strSql = string.Format("select id from allotment where tasklist_code='{0}' and hotelid='{1}' and task_code='{2}'",tasklist_code,hotelid,task_code);
           object obj= AccessHelperHolder.ExecuteScalar(strSql);
           if (string.IsNullOrEmpty(Convert.ToString(obj)))
               return string.Empty;
           else
               return Convert.ToString(obj);
        }
        private void UpdateAlloment(string hotelid, string task_code, string tasklist_code, string check,string id)
        {
            string strSql = string.Format("update allotment set hotelid='{0}',task_code='{1}' ,tasklist_code='{2}' where id='{3}'", hotelid, task_code, tasklist_code, id);
            AccessHelperHolder.ExecuteNonQuery(CommandType.Text, strSql);
        }
        private void DeleteAlloment(string hotelid, string tasklist_code)
        {
            string strSql = string.Format("delete from allotment where hotelid='{0}' and tasklist_code='{1}'",hotelid,tasklist_code);
            AccessHelperHolder.ExecuteNonQuery(CommandType.Text, strSql);
        }
        /// <summary>
        /// A000001
        /// </summary>
        /// <returns></returns>
        private string CreateNo()
        {
            string strMaxNo="A000001";
            string strSql = "select max(id) from allotment";
            object obj = AccessHelperHolder.ExecuteScalar(strSql);
            if (obj != null&&!string.IsNullOrEmpty(Convert.ToString(obj)))
            {
                strMaxNo = obj.ToString();
                int Number = Convert.ToInt32(strMaxNo.Substring(1));
                Number += 1;
                strMaxNo = string.Format("A{0}", Number.ToString().PadLeft(6, '0'));
            }
           

            return strMaxNo;
        }
    }
}